package org.spolo.android.ui;

import org.spolo.android.bean.GestureType;
import org.spolo.android.nodetree.Vcanvas;
import org.spolo.android.shape.ImageRect;
import org.spolo.android.show.R;
import org.spolo.android.util.ContextManager;

import android.graphics.RectF;
import android.util.DisplayMetrics;

/**
 * 
 */
public class UICanvas extends Vcanvas {

	private static UICanvas uiRoot = null;

	// 一级菜单
	private ImageRect rect360 = null;
	private ImageRect rectFeat = null;
	private ImageRect rectDeploy = null;
	private ImageRect rectInfo = null;
	private ImageRect rectBackgroud = null;
	// 一级菜单被激活ui
	private ImageRect rect360A = null;
	private ImageRect rectFeatA = null;
	private ImageRect rectDeployA = null;
	// 360旋转子菜单
	private ImageRect rect360_show1 = null;
	private ImageRect rect360_show2 = null;
	private ImageRect rect360_show3 = null;
	private ImageRect rect360_show4 = null;
	private ImageRect rect360_show5 = null;
	// 亮点子菜单
	private ImageRect rectFeat_1 = null;
	private ImageRect rectFeat_2 = null;
	// 安装与使用子菜单
	private ImageRect rectDeploy_1 = null;
	private ImageRect rectDeploy_2 = null;
	private ImageRect rectDeploy_3 = null;
	// 销售参考子菜单
	private ImageRect rectInfo_1 = null;
	private ImageRect rectInfo_2 = null;
	private ImageRect rectInfo_3 = null;
	private ImageRect rectInfo_4 = null;

	// 二级子菜单的提示ui
	// 360旋转子菜单
	private ImageRect rect360_show1_tip = null;
	private ImageRect rect360_show2_tip = null;
	private ImageRect rect360_show3_tip = null;
	private ImageRect rect360_show4_tip = null;
	private ImageRect rect360_show5_tip = null;
	// 亮点子菜单
	private ImageRect rectFeat_1_tip = null;
	private ImageRect rectFeat_2_tip = null;
	// 安装与使用子菜单
	private ImageRect rectDeploy_1_tip = null;
	private ImageRect rectDeploy_2_tip = null;
	private ImageRect rectDeploy_3_tip = null;
	// 销售参考子菜单
	private ImageRect rectInfo_1_tip = null;
	private ImageRect rectInfo_2_tip = null;
	private ImageRect rectInfo_3_tip = null;
	private ImageRect rectInfo_4_tip = null;
	// 一级菜单状态
	private int status = -1;
	// 二级菜单状态
	private int status_tip = -1;

	private RectF dest = null;

	private UICanvas() {
		// 获取屏幕宽度和高度设置ui的位置
		DisplayMetrics dm = ContextManager.getInstance()
				.getContext("Androidshow").getApplicationContext()
				.getResources().getDisplayMetrics();
		float startx = dm.widthPixels - 425;
		float starty = dm.heightPixels - 425;
		dest = new RectF(startx + 84, starty + 35, dm.widthPixels + 84, dm.heightPixels + 35);
		// 一级菜单
		rect360 = new ImageRect(R.drawable.chanpinzhanshi, dest);
		rectFeat = new ImageRect(R.drawable.liangdianzhanshi, dest);
		rectDeploy = new ImageRect(R.drawable.anzhuangyushiyong, dest);
		rectInfo = new ImageRect(R.drawable.xiaoshouchankao,dest);
		rectBackgroud = new ImageRect(R.drawable.beijing, dest);
		// 一级菜单被激活ui
		rect360A = new ImageRect(R.drawable.chanpinzhanshida, dest);
		rectFeatA = new ImageRect(R.drawable.liangdianzhanshida, dest);
		rectDeployA = new ImageRect(R.drawable.anzhuangyushiyongda, dest);
		// 360旋转子菜单
		rect360_show1 = new ImageRect(R.drawable.chanpinzhanshiwai1, dest);
		rect360_show2 = new ImageRect(R.drawable.chanpinzhanshiwai2, dest);
		rect360_show3 = new ImageRect(R.drawable.chanpinzhanshiwai3, dest);
		rect360_show4 = new ImageRect(R.drawable.chanpinzhanshiwai4, dest);
		rect360_show5 = new ImageRect(R.drawable.chanpinzhanshiwai5, dest);
		// 亮点子菜单
		rectFeat_1 = new ImageRect(R.drawable.liangdianzhanshiwai1, dest);
		rectFeat_2 = new ImageRect(R.drawable.liangdianzhanshiwai2, dest);
		// 安装与使用子菜单
		rectDeploy_1 = new ImageRect(R.drawable.anzhuangyushiyongwai1, dest);
		rectDeploy_2 = new ImageRect(R.drawable.anzhuangyushiyongwai2, dest);
		rectDeploy_3 = new ImageRect(R.drawable.anzhuangyushiyongwai3, dest);
		// 销售参考子菜单
		rectInfo_1 = new ImageRect(R.drawable.xiaoshouchankaowai1,dest);
		rectInfo_2 = new ImageRect(R.drawable.xiaoshouchankaowai2,dest);
		rectInfo_3 = new ImageRect(R.drawable.xiaoshouchankaowai3,dest);
		rectInfo_4 = new ImageRect(R.drawable.xiaoshouchankaowai4,dest);
		// 二级子菜单的提示ui
		// 360旋转子菜单
		rect360_show1_tip = new ImageRect(R.drawable.chanpinzhanshi1, dest);
		rect360_show2_tip = new ImageRect(R.drawable.chanpinzhanshi2, dest);
		rect360_show3_tip = new ImageRect(R.drawable.chanpinzhanshi3, dest);
		rect360_show4_tip = new ImageRect(R.drawable.chanpinzhanshi4, dest);
		rect360_show5_tip = new ImageRect(R.drawable.chanpinzhanshi5, dest);
		// 亮点子菜单
		rectFeat_1_tip = new ImageRect(R.drawable.liangdianzhanshi1, dest);
		rectFeat_2_tip = new ImageRect(R.drawable.liangdianzhanshi2, dest);
		// 安装与使用子菜单
		rectDeploy_1_tip = new ImageRect(R.drawable.anzhuangyushiyong1, dest);
		rectDeploy_2_tip = new ImageRect(R.drawable.anzhuangyushiyong2, dest);
		rectDeploy_3_tip = new ImageRect(R.drawable.anzhuangyushiyong3, dest);
		// 销售参考子菜单
		rectInfo_1_tip = new ImageRect(R.drawable.xiaoshouchankao1,dest);
		rectInfo_2_tip = new ImageRect(R.drawable.xiaoshouchankao2,dest);
		rectInfo_3_tip = new ImageRect(R.drawable.xiaoshouchankao3,dest);
		rectInfo_4_tip = new ImageRect(R.drawable.xiaoshouchankao4,dest);
		this.zIndex = 10;
		initListener();
	}

	public static UICanvas getInstance() {
		if (uiRoot == null) {
			uiRoot = new UICanvas();
		}
		return uiRoot;
	}

	private void initListener() {
		// 初始化一级菜单监听
		rect360.GetEventQueue().addListener(GestureType.onClick,
				new Turn360EventHandle());
		rectFeat.GetEventQueue().addListener(GestureType.onClick,
				new FeatureEventHandle());
		rectDeploy.GetEventQueue().addListener(GestureType.onClick,
				new DeployEventHandle());
		rectInfo.GetEventQueue().addListener(GestureType.onClick,
				new InfoEventHandle());
		rectBackgroud.GetEventQueue().addListener(GestureType.onClick,
				new BackgroudHandle());
		rect360A.GetEventQueue().addListener(GestureType.onClick,
				new Turn360EventHandle());
		rectFeatA.GetEventQueue().addListener(GestureType.onClick,
				new FeatureEventHandle());
		rectDeployA.GetEventQueue().addListener(GestureType.onClick,
				new DeployEventHandle());
		// 初始化360的二级菜单监听
		rect360_show1.GetEventQueue().addListener(GestureType.onClick,
				new ShowTip1Handle());
		rect360_show2.GetEventQueue().addListener(GestureType.onClick,
				new ShowTip2Handle());
		rect360_show3.GetEventQueue().addListener(GestureType.onClick,
				new ShowTip3Handle());
		rect360_show4.GetEventQueue().addListener(GestureType.onClick,
				new ShowTip4Handle());
		rect360_show5.GetEventQueue().addListener(GestureType.onClick,
				new ShowTip5Handle());
		// 初始化亮点的二级菜单监听
		rectFeat_1.GetEventQueue().addListener(GestureType.onClick,
				new FeatureTip1Handle());
		rectFeat_2.GetEventQueue().addListener(GestureType.onClick,
				new FeatureTip2Handle());
		// 初始化安装与使用的二级菜单监听
		rectDeploy_1.GetEventQueue().addListener(GestureType.onClick,
				new DeployTip1Handle());
		rectDeploy_2.GetEventQueue().addListener(GestureType.onClick,
				new DeployTip2Handle());
		rectDeploy_3.GetEventQueue().addListener(GestureType.onClick,
				new DeployTip3Handle());
		// 初始化销售参考的二级菜单监听
		rectInfo_1.GetEventQueue().addListener(GestureType.onClick,
				new InfoTip1Handle());
		rectInfo_2.GetEventQueue().addListener(GestureType.onClick,
				new InfoTip2Handle());
		rectInfo_3.GetEventQueue().addListener(GestureType.onClick,
				new InfoTip3Handle());
		rectInfo_4.GetEventQueue().addListener(GestureType.onClick,
				new InfoTip4Handle());
		
		// 初始化topic切换的监听
		rect360_show1_tip.GetEventQueue().addListener(GestureType.onClick,
				new Show1Handle());
		rect360_show2_tip.GetEventQueue().addListener(GestureType.onClick,
				new Show2Handle());
		rect360_show3_tip.GetEventQueue().addListener(GestureType.onClick,
				new Show3Handle());
		rect360_show4_tip.GetEventQueue().addListener(GestureType.onClick,
				new Show4Handle());
		rect360_show5_tip.GetEventQueue().addListener(GestureType.onClick,
				new Show5Handle());
		rectFeat_1_tip.GetEventQueue().addListener(GestureType.onClick,
				new Feature1Handle());
		rectFeat_2_tip.GetEventQueue().addListener(GestureType.onClick,
				new Feature2Handle());
		rectDeploy_1_tip.GetEventQueue().addListener(GestureType.onClick,
				new Deploy1Handle());
		rectDeploy_2_tip.GetEventQueue().addListener(GestureType.onClick,
				new Deploy2Handle());
		rectDeploy_3_tip.GetEventQueue().addListener(GestureType.onClick,
				new Deploy3Handle());
		rectInfo_1_tip.GetEventQueue().addListener(GestureType.onClick,
				new Info1Handle());
		rectInfo_2_tip.GetEventQueue().addListener(GestureType.onClick,
				new Info2Handle());
		rectInfo_3_tip.GetEventQueue().addListener(GestureType.onClick,
				new Info3Handle());
		rectInfo_4_tip.GetEventQueue().addListener(GestureType.onClick,
				new Info4Handle());
	}

	public void initFirstMenu() {
		rectBackgroud.zIndex = -1;
		uiRoot.addChild(rectBackgroud);
		uiRoot.addChild(rect360);
		uiRoot.addChild(rectFeat);
		uiRoot.addChild(rectDeploy);
		uiRoot.addChild(rectInfo);
		this.status = 0;
		show360Menu();
	}

	/**
	 * 显示360的二级子菜单
	 */
	public void show360Menu() {
		System.out.println("<<<<<<<<<<show360Menu>>>>>>>>");
		uiRoot.removeChild(rect360);
		uiRoot.addChild(rect360A);
		uiRoot.addChild(rect360_show1);
		uiRoot.addChild(rect360_show2);
		uiRoot.addChild(rect360_show3);
		uiRoot.addChild(rect360_show4);
		uiRoot.addChild(rect360_show5);
		this.status = 1;
	}

	/**
	 * 隐藏360的二级子菜单
	 */
	public void hide360Menu() {
		System.out.println("<<<<<<<<<<hide360Menu>>>>>>>>");
		uiRoot.removeChild(rect360A);
		uiRoot.addChild(rect360);
		uiRoot.removeChild(rect360_show1);
		uiRoot.removeChild(rect360_show2);
		uiRoot.removeChild(rect360_show3);
		uiRoot.removeChild(rect360_show4);
		uiRoot.removeChild(rect360_show5);
	}

	/**
	 * 显示亮点的二级子菜单
	 */
	public void showFeatMenu() {
		uiRoot.addChild(rectFeatA);
		uiRoot.removeChild(rectFeat);
		uiRoot.addChild(rectFeat_1);
		uiRoot.addChild(rectFeat_2);
		this.status = 2;
	}

	/**
	 * 隐藏亮点的二级子菜单
	 */
	public void hideFeatMenu() {
		uiRoot.addChild(rectFeat);
		uiRoot.removeChild(rectFeatA);
		uiRoot.removeChild(rectFeat_1);
		uiRoot.removeChild(rectFeat_2);
	}

	/**
	 * 显示安装与使用的二级子菜单
	 */
	public void showDeployMenu() {
		uiRoot.addChild(rectDeployA);
		uiRoot.removeChild(rectDeploy);
		uiRoot.addChild(rectDeploy_1);
		uiRoot.addChild(rectDeploy_2);
		uiRoot.addChild(rectDeploy_3);
		this.status = 3;
	}

	/**
	 * 隐藏安装与使用的二级子菜单
	 */
	public void hideDeployMenu() {
		uiRoot.addChild(rectDeploy);
		uiRoot.removeChild(rectDeployA);
		uiRoot.removeChild(rectDeploy_1);
		uiRoot.removeChild(rectDeploy_2);
		uiRoot.removeChild(rectDeploy_3);
	}
	
	/**
	 * 显示销售参考的二级子菜单
	 */
	public void showInfoMenu(){
		uiRoot.addChild(rectInfo_1);
		uiRoot.addChild(rectInfo_2);
		uiRoot.addChild(rectInfo_3);
		uiRoot.addChild(rectInfo_4);
		this.status = 4;
	}
	
	/**
	 * 隐藏销售参考的二级子菜单
	 */
	public void hideInfoMenu(){
		uiRoot.removeChild(rectInfo_1);
		uiRoot.removeChild(rectInfo_2);
		uiRoot.removeChild(rectInfo_3);
		uiRoot.removeChild(rectInfo_4);
	}
	
	/**
	 * 隐藏上一子菜单
	 */
	public void hideLastMenu() {
		hideLastTip();
		switch (this.status) {
		case 1:
			hide360Menu();
			break;
		case 2:
			hideFeatMenu();
			break;
		case 3:
			hideDeployMenu();
			break;
		case 4:
			hideInfoMenu();
			break;
		default:
			hideAllMenu();
			break;
		}
	}

	/**
	 * 隐藏所有子菜单
	 */
	public void hideAllMenu() {
		uiRoot.getM_children().clear();
		initFirstMenu();
	}

	/**
	 * 显示show1的提示
	 */
	public void showTip1() {
		uiRoot.addChild(rect360_show1_tip);
		this.status_tip = 11;
	}

	/**
	 * 显示show2的提示
	 */
	public void showTip2() {
		uiRoot.addChild(rect360_show2_tip);
		this.status_tip = 12;
	}

	/**
	 * 显示show3的提示
	 */
	public void showTip3() {
		uiRoot.addChild(rect360_show3_tip);
		this.status_tip = 13;
	}
	
	/**
	 * 显示show4的提示
	 */
	public void showTip4() {
		uiRoot.addChild(rect360_show4_tip);
		this.status_tip = 14;
	}
	
	/**
	 * 显示show5的提示
	 */
	public void showTip5() {
		uiRoot.addChild(rect360_show5_tip);
		this.status_tip = 15;
	}

	/**
	 * 显示feature1的提示
	 */
	public void featureTip1() {
		uiRoot.addChild(rectFeat_1_tip);
		this.status_tip = 21;
	}

	/**
	 * 显示feature2的提示
	 */
	public void featureTip2() {
		uiRoot.addChild(rectFeat_2_tip);
		this.status_tip = 22;
	}

	/**
	 * 显示deploy1的提示
	 */
	public void deployTip1() {
		uiRoot.addChild(rectDeploy_1_tip);
		this.status_tip = 31;
	}

	/**
	 * 显示deploy1的提示
	 */
	public void deployTip2() {
		uiRoot.addChild(rectDeploy_2_tip);
		this.status_tip = 32;
	}

	/**
	 * 显示deploy1的提示
	 */
	public void deployTip3() {
		uiRoot.addChild(rectDeploy_3_tip);
		this.status_tip = 33;
	}

	/**
	 * 显示info1的提示
	 */
	public void infoTip1(){
		uiRoot.addChild(rectInfo_1_tip);
		this.status_tip = 41;
	}
	
	/**
	 * 显示info1的提示
	 */
	public void infoTip2(){
		uiRoot.addChild(rectInfo_2_tip);
		this.status_tip = 42;
	}
	
	/**
	 * 显示info1的提示
	 */
	public void infoTip3(){
		uiRoot.addChild(rectInfo_3_tip);
		this.status_tip = 43;
	}
	
	/**
	 * 显示info1的提示
	 */
	public void infoTip4(){
		uiRoot.addChild(rectInfo_4_tip);
		this.status_tip = 44;
	}
	
	/**
	 * 隐藏上一提示ui
	 */
	public void hideLastTip() {
		switch (this.status_tip) {
		case 11:
			uiRoot.removeChild(rect360_show1_tip);
			break;
		case 12:
			uiRoot.removeChild(rect360_show2_tip);
			break;
		case 13:
			uiRoot.removeChild(rect360_show3_tip);
			break;
		case 14:
			uiRoot.removeChild(rect360_show4_tip);
			break;
		case 15:
			uiRoot.removeChild(rect360_show5_tip);
			break;
		case 21:
			uiRoot.removeChild(rectFeat_1_tip);
			break;
		case 22:
			uiRoot.removeChild(rectFeat_2_tip);
			break;
		case 31:
			uiRoot.removeChild(rectDeploy_1_tip);
			break;
		case 32:
			uiRoot.removeChild(rectDeploy_2_tip);
			break;
		case 33:
			uiRoot.removeChild(rectDeploy_3_tip);
			break;
		case 41:
			uiRoot.removeChild(rectInfo_1_tip);
			break;
		case 42:
			uiRoot.removeChild(rectInfo_2_tip);
			break;
		case 43:
			uiRoot.removeChild(rectInfo_3_tip);
			break;
		case 44:
			uiRoot.removeChild(rectInfo_4_tip);
			break;
		}
	}
}
